<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="vue.js"></script>
    <title>Document</title>
    <style>
        table {
            width: 500px;
            border: 1px solid #000;
            border-collapse: collapse;
        }

        td {
            border: 1px solid #000;
            width: 120px;
            text-align: center
        }

        section {
            width: 600px;
            height: 200px;
            border: 1px solid #000;
            margin: 20px 0;
            padding: 15px;
        }

        div {
            font-size: 20px
        }
    </style>
</head>

<body>
    <main id="app">
        <first-com></first-com>
        <input type="text" v-model='user'>
        <input type="text" v-model='address'>
        <section v-model='user'>
            <h1>清单</h1>
            <div>商品总价:{{price}}元</div>
            <div>收件人:{{user}}</div>
            <div>收货地址:{{address}}</div>
        </section>
    </main>

    <script>

        Vue.component("first-com", {
            template: `
            <table>
                    <tr>
                        
                        <th class="text-center">名称</th>
                        <th class="text-center">单价</th>
                        <th class="text-center">数量</th>
                        <th class="text-center">操作</th>
                    </tr>
                
                <second-com :goods="goodsList"></second-com>
            </table>
        `,
            data: function () {
                return {
                    goodsList: [
                        { one: "辣条", two: 1, three: 0, four: '删除' },
                        { one: "泡椒鸡爪", two: 1.5, three: 0, four: '删除' },
                        { one: "巧克力", two: 3, three: 0, four: '删除' }
                    ]
                }
            }

        })

        Vue.component('second-com', {
            props: ['goods'],
            template: `
            <tbody> 
                    <tr v-for="(value,index) in goods">
                           
                        <td>{{value.one}}</td>    
                        <td>{{value.two}}</td>    
                        <td>
                            <button @click="reduceClick(index)">-</button>
                            <span>{{value.three}}</span>
                            <button @click="addClick(index)">+</button>
                        </td> 
                        <td><button @click='remove(index)'>{{value.four}}</button></td>    
                    </tr>
                   
            </tbody>
         ` ,
            // data: function () {
            //     return {
            //         
            //     }
            // },
            methods: {
                addClick: function (ind) {

                    if (this.goods[ind].three < 10) {
                        this.goods[ind].three++;
                    }

                },
                reduceClick: function (ind) {

                    if (this.goods[ind].three > 0) {
                        this.goods[ind].three--;
                    }

                },
                remove: function (ind) {
                    this.goods.splice(ind, 1)
                }
            }
        })
        var vue = new Vue({
            el: '#app',
            data: {
                user: '张三',
                address: '北京',
                price:''
            },
            computed:{
                total:function(ind){
                    console.log(ind)
                    // var total = this.goods[ind].two*ind
                    // console.log(total)
                }
            }
        })
    </script>
</body>

</html>